package com.pro.framework.generator.main.generator.main;


import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.pro.framework.api.model.GeneratorDevConfig;
import com.pro.framework.generator.main.generator.AbsGenerator;
import com.pro.framework.generator.utils.StringFormater;
import com.pro.framework.javatodb.annotation.JTDTable;
import lombok.SneakyThrows;

import java.util.List;

/**
 * velocity 代码生成
 */
public class GeneratorUtil_class_platform extends AbsGenerator {

    @SneakyThrows
    public static void generate(List<Class<?>> classes) {

        // 读取 YAL
        GeneratorDevConfig generatorConfig = mapper.readValue(new ClassPathResource("system-dev.yml").getFile(), GeneratorDevConfig.class);
        init();
        String platformShortName = generatorConfig.getPlatformShortName();
//        Map<String, List<Class<?>>> moduleClassMap = classes.stream().collect(Collectors.groupingBy(c -> {
//            JTDTable annotation = c.getAnnotation(JTDTable.class);
//            AssertUtil.notEmpty(annotation, "_必须添加JTDTable注解", c);
//            String module = annotation.module();
//            AssertUtil.notEmpty(module, "_的JTDTable注解_必须添加module属性", c);
//            return module;
//        }));
//        moduleClassMap.forEach((module, subClasses) -> {
//            generatorConfig.setModule(module);
        GeneratorDevConfig generatorConfigDemo = ObjUtil.clone(generatorConfig);
        generatorConfigDemo.setModules("moduleTest");


        // 复制生成 dao service                                                                                                    D:/lzb/baoyi-cloud/platform/baoyi-services/baoyi-user-service/src/main/java/com/pro/baoyi/user/layers/DemoDao.java
        generateLayers(classes, "dao", generatorConfigDemo, generatorConfig);
        generateLayers(classes, "service", generatorConfigDemo, generatorConfig);


    }

    private static void generateLayers(List<Class<?>> classes, String layers, GeneratorDevConfig generatorConfigDemo, GeneratorDevConfig generatorConfig) {
        String layersUpperFirst = StrUtil.upperFirst(layers);
        generate(classes,
                StringFormater.format("{workspace}/{platformName}/platform/{platformShortName}-services/{platformShortName}-{moduleKebabCase}-service/src/main/java/com/pro/{platformShortName}/{moduleSeparated}/" + layers + "/EntityTest" + layersUpperFirst + ".java", generatorConfigDemo),
                (clazz) ->
                {
                    JTDTable jtdTable = clazz.getAnnotation(JTDTable.class);
                    GeneratorDevConfig generatorConfigTarget = ObjUtil.clone(generatorConfig);
                    String module;
                    if (jtdTable == null) {
                        module = "sys";
                    } else {
                        module = jtdTable.module();
                    }
                    generatorConfigTarget.setModules(module);
                    String simpleName = clazz.getSimpleName();
                    return StringFormater.format("{workspace}/{platformName}/platform/{platformShortName}-services/{platformShortName}-{moduleKebabCase}-service/src/main/java/com/pro/{platformShortName}/{moduleSeparated}/" + layers + "/" + simpleName + layersUpperFirst + ".java", generatorConfigTarget);
                },
                false);
    }
}
